#!/bin/sh -efu

DESTDIR="${DESTDIR:-/}"
pkglibdir="${pkglibdir:-/usr/lib/rpm}"

RPMRC="${1:-rpmrc}"
MACROS="${2:-macros}"
PLATFORM="${3:-platform}"

if grep /share/ $PLATFORM > /dev/null 2>&1 ; then
    DEFAULTDOCDIR='%{_usr}/share/doc'
else
    DEFAULTDOCDIR='%{_usr}/doc'
fi

#TEMPRC="/tmp/rpmrc.$$"
#cat << E_O_F > $TEMPRC
#include:	$RPMRC
#macrofiles:	$MACROS
#E_O_F
#
#RPM="./rpm --rcfile $TEMPRC"
RPM=rpm

arch="`$RPM --eval '%{_target_cpu}'`"
VENDOR="`$RPM --eval '%{_vendor}'`"
OS="`$RPM --eval '%{_os}'`"
target="`$RPM --eval '%{_target}'`"

for ARCH in noarch `grep ^arch_canon $RPMRC | cut -d: -f2`; do
  LIBSUFF=
  POINTER_SIZE=32

  PPD="${DESTDIR}/${pkglibdir}/${ARCH}-${VENDOR}-${OS}"

  [ -d $PPD ] || mkdir $PPD

  RPMRC_OPTFLAGS="`sed -n 's/^optflags: '$ARCH' //p' $RPMRC`"
  RPMRC_OPTFLAGS="`echo $RPMRC_OPTFLAGS | sed -e 's, ,\ ,g'`"

  ARCH_INSTALL_POST='%{nil}'
  case "${ARCH}-${OS}" in
    aarch64-linux)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    mipsn32-linux*)
	    LIBSUFF=32
	    ;;
    mipsn32el-linux*)
	    LIBSUFF=32
	    ;;
    mips64-linux*)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    mips64el-linux*)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    ppc64le-linux*)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    riscv64-linux)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    loongarch64-linux)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    s390x-linux)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    sparc64-linux)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    x86_64-linux)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    e2k*-linux)
	    LIBSUFF=64
	    POINTER_SIZE=64
	    ;;
    noarch-linux)
	    POINTER_SIZE=
  esac

  if [ "${ARCH}-${OS}" == "$target" ]; then
    # rpmbuild platform should match RPMs
    RPMCANONGNU="`$RPM --eval '%{_gnu}'`"
  else
    # override the defaults for specific platforms
    case "${ARCH}-${OS}" in
        armh-linux|armv8*-linux|armv7*-linux) RPMCANONGNU='-gnueabi' ;;
        *) RPMCANONGNU='-gnu' ;;
    esac
  fi

  if [ "$RPMCANONGNU" == '-gnu' ]; then
      RPMCANONGNUEABI='%nil'
  else
      RPMCANONGNUEABI="$RPMCANONGNU"
  fi

  VENDORSED=
  case $VENDOR in
  alt)
    VENDORSED='-e s,^@alt@,,'
    ;;
  pld)
    VENDORSED='-e s,^@pld@,,'
    ;;
  mandrake)
    VENDORSED='-e s,^@mandrake@,,'
    RPMRC_OPTFLAGS="`echo $RPMRC_OPTFLAGS | sed -e s/i386/i686/`"
    ;;
  conectiva)
    VENDORSED='-e s,^@conectiva@,,'
    ;;
  redhat)
    VENDORSED='-e s,^@redhat@,,'
    ;;
  esac

  LIBSUFF_EXISTS=@
  [ "$LIBSUFF" = '' ] || LIBSUFF_EXISTS=

  ARCH_ILP32=
  ARCH_LP64=
  case "$POINTER_SIZE" in
	32) ARCH_ILP32=1 ;;
	64) ARCH_LP64=1 ;;
  esac

  cat $PLATFORM \
  | sed -e "s,@RPMRC_OPTFLAGS@,$RPMRC_OPTFLAGS," \
	-e "s,@RPMRC_ARCH@,$ARCH," \
	-e "s,@LIBSUFF_DEFINITION@,${LIBSUFF:-%nil}," \
	-e "s,@LIBSUFF@,$LIBSUFF," \
	-e "s,@LIBSUFF_EXISTS@,$LIBSUFF_EXISTS," \
	-e "s,@POINTER_SIZE@,$POINTER_SIZE," \
	-e "s,@ARCH_ILP32@,$ARCH_ILP32," \
	-e "s,@ARCH_LP64@,$ARCH_LP64," \
	-e "s,@ARCH_INSTALL_POST@,$ARCH_INSTALL_POST," \
	-e "s,@DEFAULTDOCDIR@,$DEFAULTDOCDIR," \
	-e "s,@RPMCANONGNU@,$RPMCANONGNU," \
	-e "s,@RPMCANONGNUEABI@,$RPMCANONGNUEABI," \
	-e '/	$/d' \
	$VENDORSED \
  | grep -v '^@' \
  > ${PPD}/macros
done

#rm $TEMPRC
